from time import strptime, mktime, strftime
import time
import datetime
import pytz
tz = pytz.timezone('Asia/Shanghai')


class TIME:
    def __init__(self):
        pass

    @staticmethod
    def now():
        return str(TIME.time())

    @staticmethod
    def time():
        return int(time.time())

    @staticmethod
    def get_time_str(t):
        t = int(t) + (8 - time.altzone) * 3600
        # return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        # print(str(strftime("%Y-%m-%d %H:%M:%S", time.localtime(t))))
        return str(strftime("%Y-%m-%d %H:%M:%S", time.localtime(t)))

    @staticmethod
    def get_now_str():
        return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")

    @staticmethod
    def get_date_str():
        return str(datetime.date.today())

    @staticmethod
    def get_yestoday_date_str():
        yesterday = datetime.date.today() - datetime.timedelta(days=1)
        return yesterday

    @staticmethod
    def str_to_time(string):
        str_len = len(string)
        try:
            if str_len == 19:
                return int(mktime(strptime(str(string), '%Y-%m-%d %H:%M:%S')))
            elif str_len == 16:
                return int(mktime(strptime(str(string), '%Y-%m-%d %H:%M')))
        except:
            pass
        return 0

    @staticmethod
    def time_to_str(time_int):
        if time_int == 0:
            return ''
        if time_int == '':
            return ''
        try:
            time_int = int(time_int)
        except:
            time_int = int(float(time_int))

        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_int))

    @staticmethod
    def get_week():
        return time.strftime('%w', time.localtime())

    @staticmethod
    def get_now_year():
        time_array = time.localtime(int(time.time()))
        return int(time.strftime("%Y", time_array))
